
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.SysApp.DepartmentApp.Dtos;
using Core.SimpleTemp.Common.Authorization;
//using Core.SimpleTemp.Common.Core.Repository.Internal.Data;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;

namespace Core.SimpleTemp.Mvc.Api.Sys
{
    /// <summary>
    /// SysDepartment
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]
    public class SysDepartmentController : CoreApiController
    {
        private readonly SysDepartmentAppService _service;
        private readonly SysDepartmentDutyService _sddService;
        CoreDBContext _coreDBContext;
        /// <summary>
        /// SysDepartment
        ///</summary>
        public SysDepartmentController(SysDepartmentAppService service, CoreDBContext coreDBContext, SysDepartmentDutyService sddService)
        {
            _service = service;
            _sddService = sddService;
            _coreDBContext = coreDBContext;
        }

        #region 基础增删改查

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        [PermissionFilter(nameof(SysDepartmentController) + "_" + nameof(SysDepartmentController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(DepartmentPagingModel<SysDepartmentDto> pagingQueryModel)
        {
            IPageModel<SysDepartmentDto> result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }
        /// <summary>
        /// 附加工厂条件列表查询
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetListSub")]
        [PermissionFilter(nameof(SysDepartmentController) + "_" + nameof(SysDepartmentController.GetListSub), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetListSub()
        {
            DepartmentPagingModel<SysDepartmentDto> pagingQueryModel = new DepartmentPagingModel<SysDepartmentDto>();
            pagingQueryModel.Model = new SysDepartmentDto()
            {
                CreateFactory = UserIdentity.Factory
            };
            pagingQueryModel.Limit = 999;
            pagingQueryModel.Offset = 0;
            IPageModel<SysDepartmentDto> result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(SysDepartmentController) + "_" + nameof(SysDepartmentController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult> SaveAsync(SysDepartmentDto dto)
        {
            //添加 
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// 修改职务分配 
        ///</summary>
        /// <param name="dtos"></param>
        /// <returns></returns>

        [HttpPost("SaveDuty")]
        [PermissionFilter(nameof(SysDepartmentController) + "_" + nameof(SysDepartmentController.SaveDutyAsync), "保存职务分配")]
        public async Task<IActionResult> SaveDutyAsync(UpdateDepartmentDutyModel dtos)
        {
            List<SysDepartmentDutyDto> list = new List<SysDepartmentDutyDto>();
            foreach (var item in dtos.SysDutyIDs)
            {
                list.Add(new SysDepartmentDutyDto
                {
                    SysDutyID = item,
                    SysDepartmentID = dtos.SysDepartmentID
                });
            }
            await _service.UpdateDepartmentDutyAsync(dtos.SysDepartmentID, list);
            return this.JsonSuccess();
        }



        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(SysDepartmentController) + "_" + nameof(SysDepartmentController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        [PermissionFilter(nameof(SysDepartmentController) + "_" + nameof(SysDepartmentController.Get), AuthorizationConst.GET)]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            return this.JsonSuccess(model);
        }
        #endregion
    }
}
